<?php
namespace app\admin\controller;
/**
 * #投诉与反馈#{用户投诉与反馈管理}
 */
class Feedback extends Common{
    // 用户反馈
    function index($cate_id='0',$status='-1'){
        $map=[];
        if($cate_id>0){
            $map['cate_id']=$cate_id;
        }
        if($status>=0){
            $map['status']=$status;
        }
        $options=[
            'with'=>['author','cate'],
            'order'=>'s_time desc'
        ];
        $list=model('feedback')->readAll($map,true,$options);
        $params=[
            'cate_id'=>$cate_id,
            'status'=>$status,
        ];
        $list->appends($params);
        $this->assign('list',$list);
        $this->assign('params',json_encode($params));

        $cates=model('feedbackcate')->readAll(['status'=>1]);
        $this->assign('cates',$cates);
        return view();
    }
    // 答复反馈
    function reply(){
        if(request()->isAjax()||request()->isPost()){
            $data=[
                'id'=>input('id/d',0),
                'reply'=>input('reply/s',''),
            ];
            $validate=$this->validate($data,[
                'reply|答复内容'=>'require',
            ]);
            if(true!==$validate){
                $this->error($validate);
            }
            $tel='';
            if($data['id']>0){
                $has=model('feedback')->read(['id'=>$data['id']],'uid');
                if(!isset($has['uid'])){
                    $this->error('该条记录不存在或已删除');
                }
                $tel=model('member')->read(['id'=>$has['uid']],'tel');
                if(!isset($tel['tel'])||empty($tel['tel'])){
                    $this->error('该用户非微手用户或者手机号码为绑定，暂无法回复');
                }
                $tel=$tel['tel'];
            }else{
                $this->error('该条记录不存在或已删除');
            }
            $data['e_time']=time();
            $data['status']=1;
            $send=sms($tel,$data['reply']);
            if($send['code']!=0){
                $this->error($send['msg']);
            }
            $o=model('feedback')->edit($data);
            if($o){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
    // 删除反馈
    function del($id=0){
        if(request()->isAjax()||request()->isPost()){
            $ids=input('ids/a',[]); // 多项
            $map=[];
            if(empty($ids)){
                $map=['id'=>$id];
            }else{
                $map=['id'=>['id','in',$ids]];
            }
            $rs=model('feedback')->del($map);
            if($rs){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }

    // 反馈类型
    function cate(){
        $list=model('feedbackcate')->readAll([],true,['order'=>'sort,id']);
        $this->assign('list',$list);
        return view();
    }
    // 模块新增、编辑
    function cateAdd($id=0){
        if(request()->isAjax()||request()->isPost()){
            $data=[
                'name'=>input('name/s',''),
                'remark'=>input('remark/s',''),
                'status'=>input('status/d',1),
            ];
            $validate=$this->validate($data,[
                'name|分类名称'=>'require',
            ]);
            if(true!==$validate){
                $this->error($validate);
            }
            if($id>0){
                $has=model('feedbackcate')->where(['id'=>$id])->count();
                if(!$has){
                    $this->error('该条记录不存在或已删除');
                }
                
            }
            $hasName=model('feedbackcate')->where(['name'=>$data['name'],'id'=>['id','neq',$id]])->count();
            if($hasName>0){
                $this->error('该分类已经存在');
            }
            $o=false;
            if($id>0){
                $data['id']=$id;
                $o=model('feedbackcate')->edit($data);
            }else{
                $data['sort']=time();
                $o=model('feedbackcate')->add($data);
            }
            if($o){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
    // 分类删除
    function cateDel($id=0){
        if(request()->isAjax()||request()->isPost()){
            $ids=input('ids/a',[]); // 多项
            $map=[];
            if(empty($ids)){
                $has=model('feedback')->where(['cate_id'=>$id])->count();
                if($has>0){
                    $this->error('该分类下存在反馈信息，不允许删除');
                }
                $map=['id'=>$id];
            }else{
                $has=model('feedback')->where(['cate_id'=>['cate_id','in',$ids]])->count();
                if($has>0){
                    $this->error('该分类下存在反馈信息，不允许删除');
                }
                $map=['id'=>['id','in',$ids]];
            }
            $rs=model('feedbackcate')->del($map);
            if($rs){
                $this->success(lang('Successful operation'));
                die;
            }
            $this->error(lang('Failed operation'));
        }
    }
    // 分类启用、禁用
    function cateClose($id=0){
        if(request()->isAjax()||request()->isPost()){
            $ids=input('ids/a',[]); // 多项
            $rs=$this->_close('feedbackcate',$id,$ids);
            if($rs['success']){
                $this->success($rs['msg']);
                die;
            }
            $this->error($rs['msg']);
        }
    }

    // 用户联系我们列表
    public function contact($status='-1'){
        $map=[];

        if($status>=0){
            $map['status']=$status;
        }
        $options=[
            'order'=>'addtime desc'
        ];
        $list = model('contact')->readAll($map,true,$options);
        // var_dump($list->toArray());die;
        

        $params=[
            // 'cate_id'=>$cate_id,
            'status'=>$status,
        ];
        $list->appends($params);
        
        $this->assign('params',json_encode($params));
        $this->assign('list',$list);
        return view();

    }
}